package com.blaze.framework.core.boot.config;

import com.blaze.framework.core.boot.properties.ThreadPoolTaskExecutorProperties;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * 异步线程池配置
 *
 * @author created by Zhou Zongbo on 2025/5/26.
 */
@Getter
@Setter
@Configuration
@EnableConfigurationProperties(ThreadPoolTaskExecutorProperties.class)
public class ThreadPoolConfiguration {
    private final ThreadPoolTaskExecutorProperties threadPoolTaskExecutorProperties;

    public ThreadPoolConfiguration(ThreadPoolTaskExecutorProperties threadPoolTaskExecutorProperties) {
        this.threadPoolTaskExecutorProperties = threadPoolTaskExecutorProperties;
    }

    @Bean(name = "taskExecutor")
    @ConditionalOnMissingBean
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(threadPoolTaskExecutorProperties.getCorePoolSize());
        taskExecutor.setMaxPoolSize(threadPoolTaskExecutorProperties.getMaxPoolSize());
        taskExecutor.setKeepAliveSeconds(threadPoolTaskExecutorProperties.getKeepAliveSeconds());
        taskExecutor.setQueueCapacity(threadPoolTaskExecutorProperties.getQueueCapacity());
        taskExecutor.setThreadNamePrefix(threadPoolTaskExecutorProperties.getThreadNamePrefix());
        return taskExecutor;
    }
}
